<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\Storage;

class User extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        $avatar = starts_with($this->avatar, 'http://') || starts_with($this->avatar, 'https://') ?
            $this->avatar : Storage::disk('public')->url($this->avatar);
        return [
            'id' => $this->id,
            'name' => $this->name ? $this->name : __('generic.default_user_name', ['id' => $this->id]),
            'avatar' => $avatar,
            'has_setting' => $this->name !== null && $this->avatar !== null,
        ];
    }
}
